package org.android.brasil.projetos.oktoberfest2011.view;

import org.android.brasil.projetos.oktoberfest2011.R;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * 
 * @author douglas
 * 
 */
public class ComidaBebidaAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private String[] precos;
	private String[] itens;
	private Context ctx;

	public ComidaBebidaAdapter(Context context, String[] itens, String[] precos) {
		this.ctx = context;
		this.mInflater = LayoutInflater.from(context);
		this.itens = itens;
		this.precos = precos;
	}

	public int getCount() {
		return precos.length;
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder;
		if (convertView == null) {

			convertView = mInflater.inflate(R.layout.comida_bebida_list_view,
					null);

			// seta o background
			convertView.setBackgroundColor((position & 1) == 1 ? Color
					.parseColor("#FFFFFF") : Color.parseColor("#FDD1A1"));

			holder = new ViewHolder();
			holder.precoText = (TextView) convertView
					.findViewById(R.idComidaBebidaList.item);
			holder.itemText = (TextView) convertView
					.findViewById(R.idComidaBebidaList.preco);

			convertView.setTag(holder);

		} else {
			convertView.setBackgroundColor((position & 1) == 1 ? Color
					.parseColor("#FFFFFF") : Color.parseColor("#FDD1A1"));

			holder = (ViewHolder) convertView.getTag();

		}

		if (precos[position].equals("")) {
			holder.itemText.setTextSize(20);
			holder.itemText.setTextColor(Color.parseColor("#FF7D5A"));

			SpannableString content = new SpannableString(itens[position]);
			content.setSpan(new UnderlineSpan(), 0, content.length(), 0);

			holder.precoText.setText(precos[position]);
			holder.itemText.setText(content);

		} else {
			holder.itemText.setTextSize(16);
			holder.precoText.setTextSize(14);
			holder.itemText.setTextColor(Color.parseColor("#000000"));

			holder.precoText.setText(precos[position]);
			holder.itemText.setText(itens[position]);
		}

		return convertView;
	}

	static class ViewHolder {
		TextView precoText;
		TextView itemText;
	}

}
